package org.groupg.practice.guiPractice.awt;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DialogTest {
    Frame f = new Frame("测试");
    Dialog d1 = new Dialog(f, "模态对话框", true);
    Dialog d2 = new Dialog(f, "非模态对话框", false);
    Button b1 = new Button("打开模态对话框");
    Button b2 = new Button("打开非模态对话框");

    public static void main(String[] args) {
        new DialogTest().init();
    }

    public void init() {
        d1.setBounds(20, 30, 300, 400);
        d2.setBounds(20, 30, 300, 400);
        b1.addActionListener(e -> d1.setVisible(true));
        b2.addActionListener(e -> d2.setVisible(true));

        f.add(b1);
        f.add(b2, BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
        // 设置关闭监听
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        // 设置关闭监听
        d1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                d1.setVisible(false);
            }
        });
        // 设置关闭监听
        d2.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                d2.setVisible(false);
            }
        });

    }
}